package ylsb.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ylsb.model.Yiyonghaocai;
import ylsb.service.HaocaiyinjinService;
import ylsb.service.YiyonghaocaiService;
import ylsb.support.JsonResult;
import ylsb.support.PageInfo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("yiyonghaocai")
public class YiyonghaocaiController {
    @Resource
    YiyonghaocaiService ddService;

    public Logger log = LoggerFactory.getLogger(YltpBaofeiController.class);

    @RequestMapping("findyiyonghaocailist")
    public JsonResult<?> findYiyonghaocailist(@RequestParam("xiangmuid") String ppxiangmuId,@RequestParam("pageindex") int ppPageIndex,
                                              @RequestParam("pagesize") int ppPageSize){
        try {
            List<Map<String,Object>> mmHaocaiList = new ArrayList<>();
            List<Map<String,Object>> mmPinzhongAll = ddService.findPinzhongAll(ppxiangmuId,ppPageIndex,ppPageSize);

            for (int i = 0; i < mmPinzhongAll.size(); i++) {
                int mmPinzhongHebingshu = 0;
                Map<String,Object> mmMap = mmPinzhongAll.get(i);
                if (mmMap!=null){
                    List<Map<String,Object>> mmReturnPinpaiList = new ArrayList<>();
                    String mmPinzhongOne = mmMap.get("pinzhongxuhao") == null ? "" : mmMap.get("pinzhongxuhao").toString();
                    List<Map<String,Object>> mmPinpailist = ddService.findPinpaiByPinzhong(ppxiangmuId,mmPinzhongOne);
                    if (mmPinpailist.size()>0){
                        mmPinzhongHebingshu = mmPinpailist.size();
                    }else{
                        mmPinzhongHebingshu = mmPinzhongHebingshu+1;
                    }
                    List<Map<String,Object>> mmPinpaiFenlei = ddService.findPinpaiFenlei(ppxiangmuId,mmPinzhongOne);
                    for (int j = 0; j < mmPinpaiFenlei.size(); j++) {
                        int mmPinpaiHebingshu = 0;
                        Map<String,Object> mmPinpaiMap = mmPinpaiFenlei.get(j);
                        if (mmPinpaiMap!=null){
                            String mmPinpaiOne = mmPinpaiMap.get("pinpaixuhao") == null ? "" : mmPinpaiMap.get("pinpaixuhao").toString();
                            List<Map<String,Object>> mmXinhaoList = ddService.findXinghaoByPinpai(ppxiangmuId,mmPinzhongOne,mmPinpaiOne);
                            if (mmXinhaoList.size()>0){
                                mmPinpaiHebingshu = mmXinhaoList.size();
                            }else{
                                mmPinpaiHebingshu = mmPinpaiHebingshu+1;
                            }
                            mmPinpaiMap.put("xinghaolist",mmXinhaoList);
                            mmPinpaiMap.put("pinpaihebinghang",mmPinpaiHebingshu);
                            mmReturnPinpaiList.add(mmPinpaiMap);
                        }
                    }
                    mmMap.put("pinpailist",mmReturnPinpaiList);
                    mmMap.put("pinzhonghebinghang",mmPinzhongHebingshu);
                }
                mmHaocaiList.add(mmMap);
            }
            Integer mmCount = ddService.getYiyonghaocailist(ppxiangmuId,ppPageIndex,ppPageSize);
            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);

            Map<String, Object> mmReturnMap = new HashMap<String, Object>();
            mmReturnMap.put("YiyonghaocaiList", mmHaocaiList);
            mmReturnMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmReturnMap);

        }catch (Exception e) {
            e.printStackTrace();
            log.error("yiyonghaocai/findYiyonghaocailist:error",e);
            return JsonResult.getErrorResult("yiyonghaocai/findYiyonghaocailist:error " + e.getMessage());
        }

    }

    @RequestMapping("findBiaojuelist")
    public JsonResult<?> findBiaojuelist(@RequestParam("xiangmuid") String ppxiangmuId,
                                         @RequestParam("userid") String ppuserId,
                                         @RequestParam("pageindex") int ppPageIndex,
                                         @RequestParam("pagesize") int ppPageSize){
        try {
            List<Map<String,Object>> mmHaocaiList = new ArrayList<>();
            List<Map<String,Object>> mmPinzhongAll = ddService.findPinzhongAll(ppxiangmuId,ppPageIndex,ppPageSize);
            for (int i = 0; i < mmPinzhongAll.size(); i++) {
                int mmPinzhongHebingshu = 0;

                Map<String,Object> mmMap = mmPinzhongAll.get(i);
                if (mmMap!=null){
                    List<Map<String,Object>> mmReturnPinpaiList = new ArrayList<>();
                    String mmPinzhongOne = mmMap.get("pinzhongxuhao") == null ? "" : mmMap.get("pinzhongxuhao").toString();
                    List<Map<String,Object>> mmPinpailist = ddService.findPinpaiByPinzhong(ppxiangmuId,mmPinzhongOne);
                    if (mmPinpailist.size()>0){
                        mmPinzhongHebingshu = mmPinpailist.size();
                    }else{
                        mmPinzhongHebingshu = mmPinzhongHebingshu+1;
                    }
                    List<Map<String,Object>> mmPinpaiFenlei = ddService.findPinpaiResultByPinzhong(ppxiangmuId,ppuserId,mmPinzhongOne);
                    for (int j = 0; j < mmPinpaiFenlei.size(); j++) {
                        int mmPinpaiHebingshu = 0;
                        Map<String,Object> mmPinpaiMap = mmPinpaiFenlei.get(j);
                        if (mmPinpaiMap!=null){
                            String mmPinpaiOne = mmPinpaiMap.get("pinpaixuhao") == null ? "" : mmPinpaiMap.get("pinpaixuhao").toString();
                            List<Map<String,Object>> mmXinhaoList = ddService.findXinghaoByPinpai(ppxiangmuId,mmPinzhongOne,mmPinpaiOne);
                            if (mmXinhaoList.size()>0){
                                mmPinpaiHebingshu = mmXinhaoList.size();
                            }else{
                                mmPinpaiHebingshu = mmPinpaiHebingshu+1;
                            }
                            mmPinpaiMap.put("xinghaolist",mmXinhaoList);
                            mmPinpaiMap.put("pinpaihebinghang",mmPinpaiHebingshu);
                            mmReturnPinpaiList.add(mmPinpaiMap);
                        }
                    }
                    mmMap.put("pinpailist",mmReturnPinpaiList);
                    mmMap.put("pinzhonghebinghang",mmPinzhongHebingshu);

                }
                mmHaocaiList.add(mmMap);
            }
            Integer mmCount = ddService.getYiyonghaocailist(ppxiangmuId,ppPageIndex,ppPageSize);
            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);

            Map<String, Object> mmReturnMap = new HashMap<String, Object>();
            mmReturnMap.put("YiyonghaocaiList", mmHaocaiList);
            mmReturnMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmReturnMap);

        }catch (Exception e) {
            e.printStackTrace();
            log.error("yiyonghaocai/findBiaojuelist:error",e);
            return JsonResult.getErrorResult("yiyonghaocai/findBiaojuelist:error " + e.getMessage());
        }

    }

    @RequestMapping("delete")
    public JsonResult<?> delete(@RequestParam("xiangmuid") String ppxiangmuId){
        try {

            return ddService.deleteByxiangmuId(ppxiangmuId)==true? JsonResult.getSuccessResult("删除材料成功")
                    : JsonResult.getErrorResult("删除材料失败");

        }catch (Exception e) {
            e.printStackTrace();
            log.error("yiyonghaocai/delete:error",e);
            return JsonResult.getErrorResult("yiyonghaocai/delete:error " + e.getMessage());
        }
    }


}
